//
//  CollectionView.swift
//  CompareBuying
//
//  Created by 唐先才 on 16/2/19.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class CollectionView: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    var collectionView: UICollectionView?
    var dataArray: NSMutableArray = []
    var width: CGFloat?
    var height: CGFloat?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        width = frame.size.width
        height = frame.size.height
        self.createUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func passArray(dataArray: NSMutableArray) {
        self.dataArray = dataArray
        print("self.dataArray ==== \(self.dataArray)")
        guard let collectionView = self.collectionView else {
            print("刷新失败")
            return
        }
        collectionView.reloadData()
    }
    
    func createUI() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Horizontal
        guard let width = self.width, height = self.height else {
            return
        }
        layout.itemSize = CGSizeMake(width, height)
        
        let rect:CGRect = CGRectMake(0, self.frame.size.height - height - 10, width, height)
        
        collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
        guard let collectionView = self.collectionView else {
            print("创建collectionView失败")
            return
        }
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        
        let nib1 = UINib(nibName: "SpecialSmallCell", bundle: NSBundle.mainBundle())
        collectionView.registerNib(nib1, forCellWithReuseIdentifier: "specialSmallCell")
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if let specialSmallCell = collectionView.dequeueReusableCellWithReuseIdentifier("specialSmallCell", forIndexPath: indexPath) as? SpecialSmallCell {
            if self.dataArray.count > 0 {
                if let listModel = self.dataArray[indexPath.item] as? ListRecommendInfoModel {
                    specialSmallCell.configCellWithListModel(listModel)
                }
            }
            return specialSmallCell
        }
        return UICollectionViewCell()
    }
    
//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
//        return CGSizeMake(110, 130)
//    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }
}